package top.buluoluo.commoncore.domain.response;

import top.buluoluo.commoncore.constant.ResponseCode;
import lombok.Data;
import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private int code;
    private String message;
    private T data;
//    通过系统时间自动添加时间戳，方便前端排错
    private long timestamp = System.currentTimeMillis();

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResponseCode.SUCCESS.getCode());
        result.setMessage(ResponseCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failed() {
        return failed(ResponseCode.FAILED.getMessage());
    }
//    对success和failed的方法进行重载，方便后续快速使用，这个设计思想真是精妙绝伦。
    public static <T> Result<T> failed(String message) {
        return failed(ResponseCode.FAILED.getCode(), message);
    }

    public static <T> Result<T> failed(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> failed(ResponseCode responseCode) {
        return failed(responseCode.getCode(), responseCode.getMessage());
    }

    // 判断是否成功
    public boolean isSuccess() {
        return this.code == ResponseCode.SUCCESS.getCode();
    }
}